
import { put, takeEvery } from 'redux-saga/effects'
import apiType, {
    GET_WAREHOUSE_STORAGE_TYPES_SELECTION,
    GET_INVENTORY_SELECTION,
    EDIT_INVENTORY_LOSS,
    EDIT_INVENTORY_INFO,
    EDIT_INVENTORY_DRAFT,
    GET_INVENTORY_LOSS_LIST,
    GET_WAREHOUSE_STORAGE,
    GET_INVENTORY_LIST,
    GET_INVENTORY_INFO
} from "@constants/ActionTypes";
import {getAxios, postAxios, putAxios, deleteAxios} from "@utils/api";

// 查看实时库存可选类别
export const getStorageTypesSelection = (params, callback) => ({
    type: apiType(GET_WAREHOUSE_STORAGE_TYPES_SELECTION).request,
    params,
    callback
})

// 查看库存操作可选产品/商品下拉框
export const getInventorySelection = (params, callback) => ({
    type: apiType(GET_INVENTORY_SELECTION).request,
    params,
    callback
})
// 报损记录创建
export const createInventoryLoss = (params, callback) => ({
    type: apiType(EDIT_INVENTORY_LOSS).request,
    params,
    callback
})
// 提交盘点记录
export const submitInventoryInfo = (params, callback) => ({
    type: apiType(EDIT_INVENTORY_INFO).request,
    params,
    callback
})
// 取消盘点草稿
export const cancelInventoryDraft = (params, callback) => ({
    type: apiType(EDIT_INVENTORY_DRAFT).request,
    params,
    callback
})
// 查看报损记录
export const getInventoryLossList = (params, callback) => ({
    type: apiType(GET_INVENTORY_LOSS_LIST).request,
    params,
    callback
})
// 创建盘点草稿
export const creatInventoryDraft = (params, callback) => ({
    type: apiType(EDIT_INVENTORY_DRAFT).request,
    params,
    callback
})
// 查看实时库存
export const getWarehouseStorage = (params, callback) => ({
    type: apiType(GET_WAREHOUSE_STORAGE).request,
    params,
    callback
})
// 查看盘点记录列表
export const getInventoryList = (params, callback) => ({
    type: apiType(GET_INVENTORY_LIST).request,
    params,
    callback
})
// 查看盘点记录（含草稿）信息
export const getInventoryInfo = (params, callback) => ({
    type: apiType(GET_INVENTORY_INFO).request,
    params,
    callback
})


const sagas = {
    * [apiType(GET_WAREHOUSE_STORAGE_TYPES_SELECTION).request]  ({ params, callback }) {
        try {
            const response = yield getAxios('/inventory/web/selection/warehouse/storage-types', params)
            yield put({ type: apiType(GET_WAREHOUSE_STORAGE_TYPES_SELECTION).success, response });
            callback && callback()
        } catch (e) {
            yield put({ type: apiType(GET_WAREHOUSE_STORAGE_TYPES_SELECTION).failure});
        }
    },
    * [apiType(GET_INVENTORY_SELECTION).request]  ({ params, callback }) {
        try {
            const response = yield getAxios('/inventory/web/selection/object', params)
            yield put({ type: apiType(GET_INVENTORY_SELECTION).success, response });
            callback && callback()
        } catch (e) {
            yield put({ type: apiType(GET_INVENTORY_SELECTION).failure});
        }
    },
    * [apiType(EDIT_INVENTORY_LOSS).request]  ({ params, callback }) {
        try {
            const response = yield postAxios('/inventory/web/loss/info', params)
            yield put({ type: apiType(EDIT_INVENTORY_LOSS).success, response });
            callback && callback()
        } catch (e) {
            yield put({ type: apiType(EDIT_INVENTORY_LOSS).failure});
        }
    },
    * [apiType(EDIT_INVENTORY_INFO).request]  ({ params, callback }) {
        try {
            const response = yield postAxios('/inventory/web/inventory/info', params)
            yield put({ type: apiType(EDIT_INVENTORY_INFO).success, response });
            callback && callback()
        } catch (e) {
            yield put({ type: apiType(EDIT_INVENTORY_INFO).failure});
        }
    },
    * [apiType(EDIT_INVENTORY_DRAFT).request]  ({ params, callback }) {
        try {
            const response = yield deleteAxios('/inventory/web/inventory/draft', params)
            yield put({ type: apiType(EDIT_INVENTORY_DRAFT).success, response });
            callback && callback()
        } catch (e) {
            yield put({ type: apiType(EDIT_INVENTORY_DRAFT).failure});
        }
    },
    * [apiType(GET_INVENTORY_LOSS_LIST).request]  ({ params, callback }) {
        try {
            const response = yield getAxios('/inventory/web/loss/list', params)
            yield put({ type: apiType(GET_INVENTORY_LOSS_LIST).success, response });
            callback && callback()
        } catch (e) {
            yield put({ type: apiType(GET_INVENTORY_LOSS_LIST).failure});
        }
    },
    * [apiType(EDIT_INVENTORY_DRAFT).request]  ({ params, callback }) {
        try {
            const response = yield postAxios('/inventory/web/inventory/draft', params)
            yield put({ type: apiType(EDIT_INVENTORY_DRAFT).success, response });
            callback && callback()
        } catch (e) {
            yield put({ type: apiType(EDIT_INVENTORY_DRAFT).failure});
        }
    },
    * [apiType(GET_WAREHOUSE_STORAGE).request]  ({ params, callback }) {
        try {
            const response = yield postAxios('/inventory/web/warehouse/storage', params)
            yield put({ type: apiType(GET_WAREHOUSE_STORAGE).success, response });
            callback && callback()
        } catch (e) {
            yield put({ type: apiType(GET_WAREHOUSE_STORAGE).failure});
        }
    },
    * [apiType(GET_INVENTORY_LIST).request]  ({ params, callback }) {
        try {
            const response = yield getAxios('/inventory/web/inventory/list', params)
            yield put({ type: apiType(GET_INVENTORY_LIST).success, response });
            callback && callback()
        } catch (e) {
            yield put({ type: apiType(GET_INVENTORY_LIST).failure});
        }
    },
    * [apiType(GET_INVENTORY_INFO).request]  ({ params, callback }) {
        try {
            const response = yield getAxios('/inventory/web/inventory/info', params)
            yield put({ type: apiType(GET_INVENTORY_INFO).success, response });
            callback && callback(response)
        } catch (e) {
            yield put({ type: apiType(GET_INVENTORY_INFO).failure});
        }
    },
}
export default Object.keys(sagas).map(item => {
    return function * s() {
        yield takeEvery(item, function *(args) {
            try {
                yield sagas[item](args)
            } catch (e) {
                console.log(e)
            }
        })
    }
})